// File Name: ProviderFragmentRootCustom.cpp

#include "ProviderFragmentRootCustom.h"
//#include "RotateClientSideCustomProvider.h"

namespace UIAutomationCPP
{
	ProviderFragmentRootCustom::ProviderFragmentRootCustom()
	{
	}

	ProviderFragmentRootCustom::~ProviderFragmentRootCustom()
	{
	}

	HRESULT __stdcall ProviderFragmentRootCustom::ElementProviderFromPoint(double x, double y, IRawElementProviderFragment** pRetVal)
	{
		if (pRetVal == NULL)
			return E_INVALIDARG;
		
		POINT pt;
		pt.x = (LONG)x;
		pt.y = (LONG)y;
		ScreenToClient(controlHwnd, &pt);
		/*int itemIndex = this->m_pControl->IndexFromY(controlHwnd, pt.y);
		RotateClientSideCustomProvider* pItem = *pRetVal;  
		if (pItem != NULL)
		{
			*pRetVal = (IRawElementProviderFragment*)pItem;
			pItem->AddRef();
		}
		else 
		{
			*pRetVal = (IRawElementProviderFragment*)this;
			pItem->AddRef();
		}*/

		return S_OK;
	}

	HRESULT __stdcall ProviderFragmentRootCustom::GetFocus(IRawElementProviderFragment** pRetVal)
	{
		if (pRetVal == NULL)
			return E_INVALIDARG;
		
		/*RotateClientSideCustomProvider* pItem = GetItemByIndex(m_pControl->m_focusedItem); 
		pItem->AddRef();
		*pRetVal = (IRawElementProviderFragment*)pItem;*/
		return S_OK;
	}

	HRESULT __stdcall ProviderFragmentRootCustom::QueryInterface(REFIID riid, void **ppvObj)
	{
		if(riid == IID_IUnknown)
			*ppvObj =(IUnknown*)((IRawElementProviderFragmentRoot*)this);
		else if(riid == IID_IRawElementProviderFragmentRoot)
			*ppvObj =(IRawElementProviderFragmentRoot*)this;
		else
		{
			*ppvObj = NULL;
			return E_NOINTERFACE;
		}

		((IUnknown*)(*ppvObj))->AddRef();
		return S_OK;
	}

	ULONG __stdcall ProviderFragmentRootCustom::AddRef()
	{
		InterlockedIncrement(&refCount);
		return refCount;
	}

	ULONG __stdcall ProviderFragmentRootCustom::Release()
	{
		ULONG ulRefCount = InterlockedDecrement(&refCount);
		if (ulRefCount == 0)
		{
			delete this;
		}
		return ulRefCount;
	}
}
